<?php

namespace Modules\Admin\Http\Controllers\Login;


use Modules\Common\General\Login\BaseLoginController;
use Modules\Common\Models\AdminUser;
use Modules\Common\Util\Api;
use Laravel\Passport\TokenRepository;
use Illuminate\Http\Request;

class Login extends BaseLoginController
{

    /**
     * @Route(path=/api/login/admin)
     */
    public function login()
    {
        $user = AdminUser::where('mobile', request('mobile'))->first();
        if ($user === null) {
            return Api::illegalLogin();
        }
        //验证码登录
        if (request('code')) {
            return $this->loginByCode($user, 'admin-login');
        }
        //密码登录
        if (request('password')) {
            return $this->loginByPassword($user);
        }
        return Api::illegalLogin();
    }


    /**
     * 解析前端传过来的 token 值
     * @Route(path=/api/login/get-token)
     */
    public function parseToken()
    {
        // $token = request('token');
        $token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiMjBmMTAzMTEzMWI2NDIzYjQ4OGNiNGYyN2E1YjZmNmNjMjg4MmY0OWJjZmY3Njg1NDJlNjUxODc5NDJjM2U2MmI4YmJjZGU2MmI3YTlmYmEiLCJpYXQiOjE3MjU2MDAzNzksIm5iZiI6MTcyNTYwMDM3OSwiZXhwIjoxNzI2ODk2Mzc4LCJzdWIiOiIzNzMiLCJzY29wZXMiOltdfQ.YRM785FNl4ZgerGbrFxjsd2YiQFhxv0-v-sz5W6WeAFAb2or4RnRWPRzFQASl1BCrAKWFSoMcoIrgG8B3O5-iFZyEQiEsYi9jrceRsp4kRXKodfRtIPjMOIuBbn7gsm-26ndGMKJV5tbNkWNUHjLoh0iy_lVHa0WAAg6RWKhmYEFmJMxgLcl2BxJEi330vYqH88FmCfkqO3yGKJ6m5l11u2hGGILXsoUkMiolBtpowDJiI7PgjWlCO5czx7vf5kdaTjYSVfElLO85R2VHkBfQz8apRhtgjKg24gm_LS0IEjEl_S7WoY6a3abqj10OrOyvtPQMTQMk8Mjyj-OIsz-yffGqF_1mgqm7pgvASeidgJHgPD7XSA08Wzjh_Is1DMuSx-fWSOBFwzjrLO6Z-bj7dgkJp9bJxTnbTBXWNY5yMUQ3xK77A1WEPOFdTUHmyITB_x6c5iOCOPvPjqQIju6pnvPy2SSdcGGTitYerwbAvSXwm1-IG-NacADWvfgZiDQysmcamLqdUCkdzn-f7T11g-vxHd_5HIF0-5oW6d5Lo8oDsoaNnjF91NLKP0wbWJs_rXEbOF5z0ebWxWxKOHQTk9aaQJleCi7PwRNF8N1ot2m52ljYSc2LiAirPhyYQLJXNJ6ZMLZc2di8afZXYB7TtmxUdu54oQ8NODBKdMyEaY";
            try {
                // 解析 Token
                $tokenRepository = new TokenRepository();
                // dd($tokenRepository);
                $accessToken = $tokenRepository->find($token);
                if ($accessToken) {
                    // 令牌有效，你可以获取用户信息
                    $user = $accessToken->user;
                    // 执行你的逻辑...
                    echo 'User ID: ' . $user->id;
                } else {
                    // 令牌无效或不存在
                    echo 'Invalid or absent token';
                }
            } catch (\Exception $e) {
                // 处理异常
                echo 'Error: ' . $e->getMessage();
            }
    }

}
